package com.zt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.service.CardBindingService;
import com.zt.system.CardBinding;
import com.zt.system.Transfer;
import com.zt.service.TransferService;
import com.zt.mapper.TransferMapper;
import com.zt.user.UserFeign;
import com.zt.utils.Result;
import com.zt.vo.CardBindingVo;
import com.zt.vo.TransactionRecordVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
* @author 不.凌
* @description 针对表【transfer】的数据库操作Service实现
* @createDate 2024-09-03 17:43:26
*/
@Service
public class TransferServiceImpl extends ServiceImpl<TransferMapper, Transfer>
    implements TransferService{

    @Resource
    private CardBindingService cardBindingService;

    @Resource
    private UserFeign userFeign;

    @Override
    @Transactional
    public Result TransferAccounts(CardBindingVo cardBindingVo) {
        double fee;
        if (cardBindingVo.getBalance() <= 5000) {
            fee = 0;  // 5000元以下免手续费
        } else if (cardBindingVo.getBalance() <= 10000) {
            fee = 5;  // 5000-10000元区间，每笔收取5元
        } else if (cardBindingVo.getBalance() <= 50000) {
            fee = 7;  // 10000-50000元区间，每笔收取7元
        } else {
            fee = cardBindingVo.getBalance() * 0.00015;  // 50000元以上按0.015%收取
            if (fee > 25) {
                fee = 25;  // 但是手续费最高不超过25元
            }
        }
        //付款账户
        CardBinding cardBinding = cardBindingService.getOne(Wrappers.<CardBinding>lambdaQuery().eq(CardBinding::getCardNumber, cardBindingVo.getCardNumber()));
        if(cardBinding==null){
            return Result.error("付款账户不存在");
        }
        //收款账户
        CardBinding cardBinding1 = cardBindingService.getOne(Wrappers.<CardBinding>lambdaQuery().eq(CardBinding::getCardNumber, cardBindingVo.getSardNumber()));
        if(cardBinding1==null){
            return Result.error("收款账户不存在");
        }
        if(cardBindingVo.getCardNumber().equals(cardBindingVo.getSardNumber())){
            return Result.error("不能转账给自己");
        }
        if(cardBindingVo.getBalance()==0){
            return Result.error("转账金额不能为零");
        }
        if(cardBindingVo.getBalance()>cardBinding.getBalance()){
            return Result.error("当前余额不足，无法转账");
        }
        //收款
        if(cardBinding1.getCardType()!=3){
            cardBinding1.setBalance(cardBinding1.getBalance()+cardBindingVo.getBalance());
            cardBindingService.updateById(cardBinding1);
        }else {
            return Result.error("收款银行卡已被冻结，无法使用");
        }
        //付款
        if(cardBinding.getBankNumber().equals(cardBindingVo.getBankNumber())){
            if(cardBinding.getCardType()!=3){
                //减去转账金额和手续费
                cardBinding.setBalance((int) (cardBinding.getBalance()-cardBindingVo.getBalance()-fee));
                cardBindingService.updateById(cardBinding);
            }else {
                return Result.error("付款银行卡已被冻结，无法使用");
            }
        }else {
            return Result.error("银行卡密码错误");
        }
        Transfer transfer = new Transfer();
        transfer.setSenderUserId(cardBinding.getUserId());
        transfer.setReceiverUserId(cardBinding1.getUserId());
        transfer.setAmount(BigDecimal.valueOf(cardBindingVo.getBalance()));
        transfer.setTransferDate(new Date());
        this.save(transfer);
        return Result.success("转账成功,你需要支付手续费"+fee);
    }

    @Override
    public Result PageTransfer(TransactionRecordVo transactionRecordVo) {
        Page<Transfer> page = new Page<>(transactionRecordVo.getPageNum(), transactionRecordVo.getPageSize());
        LambdaQueryWrapper<Transfer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.checkValNotNull(transactionRecordVo.getSenderUserId()),Transfer::getSenderUserId,transactionRecordVo.getSenderUserId());
        this.page(page,wrapper);
        for (Transfer record : page.getRecords()) {
            //转出用户
            String sname = userFeign.getUserById(record.getSenderUserId());
            //转入用户
            String rname = userFeign.getUserById(record.getReceiverUserId());
            record.setSname(sname);
            record.setRname(rname);
        }
        return Result.success(page);
    }
}


